/*
 * jBeatsLogin.java
 *
 * Created on Nov 13, 2012, 7:56:54 AM
 */
package cs341.cs.uwosh.edu.jBeats;
import java.awt.event.KeyEvent;

/**
 *
 * @author haibut41
 */
public class jBeatsLogin extends javax.swing.JFrame {
    String username;
    String password;
    User[] users;
    FrameCreator FC = new FrameCreator();
    public jBeatsMain frame;
    /** Creates new form jBeatsLogin */
    public jBeatsLogin() {
        initComponents();
        users = DatabaseInfo.getUserList();
//        System.out.println("Showing userlist");
//        for(int i=0;i<users.length;i++){
//            System.out.println(users[i]);
//        }
    }

    jBeatsLogin(JBeatsApp aThis) {
        initComponents();
        users = DatabaseInfo.getUserList();
//        System.out.println("Showing userlist");
//        for(int i=0;i<users.length;i++){
//            System.out.println(users[i]);
//        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginPanel = new javax.swing.JPanel();
        loginInfoPanel = new javax.swing.JPanel();
        usernameLabel = new javax.swing.JLabel();
        usernameField = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        loginButton = new javax.swing.JButton();
        passwordField = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        loginMenuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("jBeats");

        loginPanel.setBackground(new java.awt.Color(84, 131, 161));
        loginPanel.setName("loginPanel"); // NOI18N

        loginInfoPanel.setBackground(new java.awt.Color(84, 116, 191));
        loginInfoPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(255, 102, 0), null));
        loginInfoPanel.setName("loginInfoPanel"); // NOI18N

        usernameLabel.setText("Username:");
        usernameLabel.setName("usernameLabel"); // NOI18N

        usernameField.setName("usernameField"); // NOI18N

        passwordLabel.setText("Password:");
        passwordLabel.setName("passwordLabel"); // NOI18N

        loginButton.setText("Login");
        loginButton.setName("loginButton"); // NOI18N
        loginButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                loginClicked(evt);
            }
        });

        passwordField.setName("passwordField"); // NOI18N
        passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pressedEnter(evt);
            }
        });

        javax.swing.GroupLayout loginInfoPanelLayout = new javax.swing.GroupLayout(loginInfoPanel);
        loginInfoPanel.setLayout(loginInfoPanelLayout);
        loginInfoPanelLayout.setHorizontalGroup(
            loginInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginInfoPanelLayout.createSequentialGroup()
                .addGroup(loginInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginInfoPanelLayout.createSequentialGroup()
                        .addGap(207, 207, 207)
                        .addComponent(loginButton))
                    .addGroup(loginInfoPanelLayout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addGroup(loginInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(passwordLabel)
                            .addComponent(usernameLabel)
                            .addGroup(loginInfoPanelLayout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE))
                            .addGroup(loginInfoPanelLayout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(passwordField)))))
                .addContainerGap(164, Short.MAX_VALUE))
        );
        loginInfoPanelLayout.setVerticalGroup(
            loginInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(usernameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(loginButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setIcon(new javax.swing.ImageIcon("src/cs341/cs/uwosh/edu/jBeats/logo384.png")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(loginInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addGap(203, 203, 203)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(154, Short.MAX_VALUE))
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginPanelLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(loginInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        loginMenuBar.setName("loginMenuBar"); // NOI18N

        jMenu1.setText("File");
        jMenu1.setName("jMenu1"); // NOI18N
        loginMenuBar.add(jMenu1);

        jMenu2.setText("Edit");
        jMenu2.setName("jMenu2"); // NOI18N
        loginMenuBar.add(jMenu2);

        jMenu3.setText("Help");
        jMenu3.setName("jMenu3"); // NOI18N
        loginMenuBar.add(jMenu3);

        setJMenuBar(loginMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(loginPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void loginClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_loginClicked
    username = usernameField.getText();
    password = passwordField.getText();
    
    if(verifyLogin(username,password)){
      frame = FrameCreator.createMain(username);
      frame.setVisible(true);
      this.dispose();
      System.out.println("Login successful");
    }else
        System.out.println("Invalid Login");
    
    //String[] test = databaseInfo.getSongList("schled84", "PL2");
    //for(int i=0;i<test.length;i++)
    //    System.out.println(test[i]);
    
}//GEN-LAST:event_loginClicked

private void pressedEnter(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pressedEnter
    //Press button when user presses enter
    int pres = evt.getKeyCode();
    if(pres==KeyEvent.VK_ENTER)
    loginClicked(null);
    
    
}//GEN-LAST:event_pressedEnter

    private boolean verifyLogin(String userName, String pass){
        boolean ret1 = false;
        boolean ret2 = false;
        for(int i=0;i<users.length;i++){
            if(userName.equals(users[i].getUserName()))
            {
                ret1 = true;
                if (pass.equals(DatabaseInfo.getPassword(userName)))
                    ret2=true;
            }
        }

        return (ret1 && ret2);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(jBeatsLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(jBeatsLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(jBeatsLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(jBeatsLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new jBeatsLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JButton loginButton;
    private javax.swing.JPanel loginInfoPanel;
    private javax.swing.JMenuBar loginMenuBar;
    private javax.swing.JPanel loginPanel;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JTextField usernameField;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables
}
